package com.cis.web.controller.api;

import com.cis.common.core.controller.BaseController;
import com.cis.common.core.domain.AjaxResult;
import com.cis.education.domain.*;
import com.cis.education.service.*;
import com.cis.framework.jwt.utils.JwtUtils;
import com.cis.framework.shiro.service.SysPasswordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("教师API接口")
@Controller
@RequestMapping("/api/teacher")
@ResponseBody
public class TeacherApiController extends BaseController {

    public static final String TYPE_TEACHER = "教师";

    @Autowired
    IEduTeacherService eduTeacherService;

    @Autowired
    IEduStudentService eduStudentService;

    @Autowired
    IEduCourseService eduCourseService;

    @Autowired
    IEduCheckinService eduCheckinService;

    @Autowired
    IEduClassroomService eduClassroomService;

    @Autowired
    IEduCourseTeacherService eduCourseTeacherService;

    @Autowired
    IEduCourseStudentService eduCourseStudentService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    IEduCheckinStudentService eduCheckinStudentService;

    /**
     * 教师登录
     *
     * @param eduTeacher
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("教师登录接口")
    public AjaxResult login(EduTeacher eduTeacher){
        EduTeacher eduTeacherDB = eduTeacherService.selectEduTeacherById(eduTeacher.getId());
        if (eduTeacherDB == null) {
            return AjaxResult.error("登陆失败");
        }

        //判断密码是否正确
        if(passwordService.matchTeacher(eduTeacherDB,eduTeacher.getPassword())){
            //登录成功获取Token
            String token = JwtUtils.createToken(eduTeacherDB.getId(), eduTeacherDB.getPassword(), TYPE_TEACHER);
            eduTeacherDB.setToken(token);
            return AjaxResult.success(eduTeacherDB);
        }
        return AjaxResult.error("登陆失败");
    }

    /**
     * 教师个人信息修改
     */
    @PostMapping("/person")
    @ApiOperation("教师个人信息修改")
    public AjaxResult information(EduTeacher eduTeacher){
        if(eduTeacherService.updateEduTeacher(eduTeacher) != 0){
            return AjaxResult.success(eduTeacherService.selectEduTeacherById(eduTeacher.getId()).setToken(eduTeacher.getToken()));
        }
        return AjaxResult.error();
    }

    /**
     * 教师修改密码
     */
    @PostMapping("/password")
    public AjaxResult password (EduTeacher eduTeacher, String password){
        eduTeacher.setPassword(passwordService.encryptPassword(
                eduTeacher.getName(),password,eduTeacher.getSalt()
        ));
        return toAjax(eduTeacherService.updateEduTeacher(eduTeacher));
    }


    /**
     * 根据教师获取课程
     */
    @GetMapping("/courses")
    public AjaxResult getCourseByTeacherId(EduTeacher eduTeacher){
        EduCourseTeacher eduCourseTeacher = new EduCourseTeacher();
        eduCourseTeacher.setTeacherId(eduTeacher.getId());
        List<EduCourseTeacher> eduCourseTeachers = eduCourseTeacherService.selectEduCourseTeacherList(eduCourseTeacher);

        List<EduCourse> eduCourses = new ArrayList<>();

        if(!eduCourseTeachers.isEmpty()){
            for (EduCourseTeacher ect : eduCourseTeachers){
                EduCourse eduCourse = eduCourseService.selectEduCourseById(ect.getCourseId());
                eduCourses.add(eduCourse);
            }
        }

        return AjaxResult.success(eduCourses);
    }

    /**
     * 根据课程获取选课学生名单
     */
    @GetMapping("/course/{courseId}/students")
    public AjaxResult getStudentsByCourseId(@PathVariable Integer courseId){
        EduCourseStudent eduCourseStudent = new EduCourseStudent();
        eduCourseStudent.setCourseId(courseId);
        List<EduCourseStudent> eduCourseStudents = eduCourseStudentService.selectEduCourseStudentList(eduCourseStudent);

        List<EduStudent> eduStudents = new ArrayList<>();

        if(!eduCourseStudents.isEmpty()){
            for (EduCourseStudent ecs : eduCourseStudents) {
                EduStudent eduStudent = eduStudentService.selectEduStudentById(ecs.getStudentId());
                eduStudents.add(eduStudent);
            }
        }
        return AjaxResult.success(eduStudents);
    }

    /**
     * 查看学生信息
     */
    @GetMapping("/student/{studentId}")
    public AjaxResult getStudentByStudentId(@PathVariable String studentId){
        return AjaxResult.success(eduStudentService.selectEduStudentById(studentId));
    }

    /**
     * 获取教室信息
     */
    @GetMapping("/classrooms")
    public AjaxResult getClassroom(){
        return AjaxResult.success(eduClassroomService.selectEduClassroomList(new EduClassroom()));
    }
    /**
     * 创建签到
     */
    @PostMapping("/checkin")
    public AjaxResult createCheckin(EduCheckin eduCheckin)
    {
        return toAjax(eduCheckinService.insertEduCheckin(eduCheckin));
    }

    /**
     * 取消当前正在进行中的签到
     */
    @PostMapping("checkin/cancel/{checkinId}")
    public AjaxResult cancel(@PathVariable Integer checkinId){
        EduCheckin eduCheckin = eduCheckinService.selectEduCheckinById(checkinId);
        eduCheckin.setStatus(2);
        return toAjax(eduCheckinService.updateEduCheckin(eduCheckin));
    }

    /**
     * 获取历史签到信息
     */
    @GetMapping("/checkins")
    public AjaxResult getCheckinListByTeacherId(String teacherId){

        EduCheckin eduCheckin = new EduCheckin();
        eduCheckin.setTeacherId(teacherId);
        List<EduCheckin> eduCheckins = eduCheckinService.selectEduCheckinList(eduCheckin);

        for (EduCheckin ec : eduCheckins){
            ec.setTeacher(eduTeacherService.selectEduTeacherById(ec.getTeacherId()));
            ec.setCourse(eduCourseService.selectEduCourseById(ec.getCourseId()));
            ec.setClassroom(eduClassroomService.selectEduClassroomById(ec.getClassroomId()));
        }
        return AjaxResult.success(eduCheckins);
    }

    /**
     * 获取签到详细信息
     */
    @GetMapping("/checkin/{checkinId}")
    public AjaxResult getCheckinByCheckinId(@PathVariable Integer checkinId){

        EduCheckin eduCheckin = eduCheckinService.selectEduCheckinById(checkinId);

        if (eduCheckin != null){
            //添加签到创建教师信息
            eduCheckin.setTeacher(eduTeacherService.selectEduTeacherById(eduCheckin.getTeacherId()));
            //添加教室信息
            eduCheckin.setClassroom(eduClassroomService.selectEduClassroomById(eduCheckin.getClassroomId()));
            //添加签到课程信息
            eduCheckin.setCourse(eduCourseService.selectEduCourseById(eduCheckin.getCourseId()));

            //添加签到学生列表
            EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
            eduCheckinStudent.setCheckinId(checkinId);

            //签到学生列表
            List<EduCheckinStudent> totalStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);

            eduCheckin.setTotalNum(totalStudents.size());

            List<EduStudent> studentList = new ArrayList<>();

            for (int i = 0; i < totalStudents.size(); i++) {
                EduStudent eduStudent = eduStudentService.selectEduStudentById(totalStudents.get(i).getStudentId());
                eduStudent.setCheckinStatus(totalStudents.get(i).getStatus());
                studentList.add(eduStudent);
            }

            eduCheckin.setStudentList(studentList);

            //已签到学生列表

            eduCheckinStudent.setStatus(1);

            List<EduCheckinStudent> attendStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);

            eduCheckin.setAttendNum(attendStudents.size());


            return AjaxResult.success(eduCheckin);
        }

        return AjaxResult.error();
    }

    /**
     * 学生补签
     */
    @PostMapping("/checkin/{checkinId}/student/{studentId}")
    @ResponseBody
    public AjaxResult supplyCheckin(@PathVariable Integer checkinId, @PathVariable String studentId){
        EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
        eduCheckinStudent.setCheckinId(checkinId);
        eduCheckinStudent.setStudentId(studentId);
        List<EduCheckinStudent> eduCheckinStudents = eduCheckinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        if (eduCheckinStudents.isEmpty()){
            return AjaxResult.error("补签失败，该同学已成功签到！");
        }else{
            eduCheckinStudents.get(0).setStatus(1);
            eduCheckinStudentService.updateEduCheckinStudent(eduCheckinStudents.get(0));
            return AjaxResult.success("已成功补签！");
        }
    }

}
